//
//  LCTurnMap.m
//  LCself.150115self.1
//
//  Created by Charlie. on 16/1/16.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "LCTurnMap.h"

@interface LCTurnMap ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;/**< 轮播图 */
@property (nonatomic, retain) UIPageControl *page;/**< 小圆点 */
@property (nonatomic, retain) NSTimer *timer;/**< 时间 */
@property (nonatomic, assign) NSInteger index; /**< 全局变量 */
@property (nonatomic, retain) NSMutableArray* arr; /**<接受Arr*/

@end

@implementation LCTurnMap

- (void)dealloc
{
    [self.scrollView release];
    [self.page release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame URLArr:(NSMutableArray *)Arr timeInterval:(NSTimeInterval)ti Yuandianframe:(CGRect)frame2
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] init];
        [self addSubview:self.scrollView];
        [self.scrollView release];
        self.page = [[UIPageControl alloc] init];
        [self addSubview:self.page];
        [self.page release];
        // 传入Frame
        self.scrollView.frame = frame;
        // 代理人
        self.scrollView.delegate = self;
        // 整页滚
        self.scrollView.pagingEnabled = YES;
        
        // 加入第一张图片 左后一张图片
        id firstImage = [Arr firstObject];
        id lastImage = [Arr lastObject];
        
        [Arr addObject:firstImage];
        [Arr insertObject:lastImage atIndex:0];
        // 滚动大小
        self.scrollView.contentSize = CGSizeMake(self.bounds.size.width * Arr.count, 0);
        self.scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        
        // 轮播图
        for (NSInteger i = 0; i < Arr.count; i++) {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width * i, 0, self.bounds.size.width, self.frame.size.height)];
            [imgView sd_setImageWithURL:[NSURL URLWithString:Arr[i]]];
            [self.scrollView addSubview:imgView];
            [imgView release];
        }
        
        // 小圆点
        self.page = [[UIPageControl alloc] initWithFrame:frame2];
        self.page.center = CGPointMake(self.center.x, self.page.center.y);
        self.page.numberOfPages = Arr.count - 2;
        [self addSubview:self.page];
        [self.page addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        [self.page release];

        self.timer = [NSTimer scheduledTimerWithTimeInterval:ti target:self selector:@selector(zidong:) userInfo:nil repeats:YES];
        self.index = 0;
        self.arr = Arr;
    }
    return self;
}
-(void)zidong:(NSTimer *)timer
{
    
    //使用scrollview自带的滚动动画,实现起来效果不好,有bug.所以需要自己写动画效果
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.scrollView setContentOffset:CGPointMake((self.index + 1) * self.scrollView.frame.size.width, 0)];
    }];
    self.index++;
    //如果滑动到最后一页
    if (self.scrollView.contentOffset.x == (self.arr.count - 1) * self.scrollView.frame.size.width) {
        //快速移动到第一页
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
        
        self.index = 0;
        
    }
    
}
- (void)changePage:(UIPageControl *)page
{
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.scrollView.frame.size.width, 0) animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.page.currentPage = (scrollView.contentOffset.x - self.scrollView.frame.size.width) / scrollView.frame.size.width;
    
}
//当手指接触的时候让定时器停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = nil;
}
//当手指离开的时候开始滑动
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(zidong:) userInfo:nil repeats:YES];
    
}

//定时器在不断循环滚动,就不会减速停止,此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.scrollView == scrollView) {
        
        //每次减速结束都让scollView所有的scrollView子视图缩放变为1
        for (UIScrollView * scrollv in scrollView.subviews) {
            if ([scrollv isKindOfClass:[UIScrollView class]]) {
                scrollv.zoomScale = 1.0;
            }
        }
        
        
        if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width * (self.arr.count - 1)) {
            
            NSLog(@"最后一页");//循环滚动
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
        }
        
        if (self.scrollView.contentOffset.x <= 0) {
            
            
            
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (self.arr.count - 2), 0) animated:NO];
        }
        
    }
    //不管怎么移动,保证self.index存的是当前的页码
    self.index = self.scrollView.contentOffset.x / self.scrollView.frame.size.width -1;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
